package br.org.blacklist.web.governamental;

import org.apache.wicket.feedback.ContainerFeedbackMessageFilter;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;

import com.google.inject.Inject;

import br.org.blacklist.core.jdo.repository.IBlacklistRepository;
import br.org.blacklist.model.governamental.Instituicao;
import br.org.blacklist.service.governamental.InstituicaoQueryInterface;
import br.org.blacklist.web.componentes.BlacklistFeedbackPanel;
import br.org.blacklist.web.componentes.InstituicaoPainelFormulario;
import br.org.blacklist.web.template.BlacklistTemplateWebPageAbstract;

public class MantemInstituicaoWebPage extends BlacklistTemplateWebPageAbstract {

    private static final long serialVersionUID = 1L;

    @Inject
    private IBlacklistRepository<Instituicao> repository;

    @Inject
    private InstituicaoQueryInterface query;

    public MantemInstituicaoWebPage() {
        final Form<Instituicao> formulario = new Form<Instituicao>("formulario");
        InstituicaoPainelFormulario painel = new InstituicaoPainelFormulario("painel-instituicao", new Instituicao());
        formulario.add(painel);
        formulario.getModelObject();
        formulario.add(painel.new CancelarLink("cancelar"));
        formulario.add(new Button("enviar") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                try {
                    Instituicao instituicao = formulario.getModelObject();
                    MantemInstituicaoWebPage.this.repository.persist(instituicao);
                    formulario.clearInput();
                    success(getString(MENSAGEM_SUCESSO));
                } catch (Exception e) {
                    error(MENSAGEM_ERRO);
                }
            }
        });
        add(new BlacklistFeedbackPanel("feedback", new ContainerFeedbackMessageFilter(formulario)));
        add(formulario);
    }

}
